package com.xtedu.examproject.controller;

import com.xtedu.examproject.entity.Menu;
import com.xtedu.examproject.entity.Subject;
import com.xtedu.examproject.service.MenuService;
import com.xtedu.examproject.service.SubjectService;
import com.xtedu.examproject.util.MenuTreeBuilder;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/dashboard")
public class DashboardServlet extends HttpServlet {
    private MenuService menuService;
    private SubjectService subjectService;


    @Override
    public void init() throws ServletException {
        menuService = new MenuService();
        subjectService = new SubjectService();
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        String content = req.getParameter("content");
        if (content != null) {
            content += ".jsp";
            req.setAttribute("contentPage", content);
        }

        int roleId = Integer.parseInt(req.getParameter("roleId"));
        int userId = Integer.parseInt(req.getParameter("userId"));
        int infoId = Integer.parseInt(req.getParameter("infoId"));
        // 携带导航信息及用户角色ID
        try {
            List<Menu> menus = menuService.findMenusByRoleId(roleId);
            List<Menu> treeMenus = MenuTreeBuilder.buildMenuTree(menus);
            req.setAttribute("menus", treeMenus);
            req.setAttribute("roleId", roleId);
            req.setAttribute("userId", userId);
            req.setAttribute("infoId", infoId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //进行判断，因为有两次请求
        if (content != null && content.equals("coursemanage.jsp")) {
            try {
                List<Subject> subjects = subjectService.findAllSubject();
                req.setAttribute("subjects", subjects);
                List<String> subjectNames = subjects.stream().map(Subject::getName).toList();
                req.setAttribute("subjectNames", subjectNames);

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        // 路由
        if (content != null && content.equals("showActive.jsp")) {
            req.getRequestDispatcher("/user/showActive").forward(req, resp);
        } else if (content != null && content.equals("showUserInfo.jsp")) {
            req.getRequestDispatcher("/user/showUserInfo").forward(req, resp);
        } else if (content != null && content.equals("adduser.jsp")) {
            req.getRequestDispatcher("/user/addUser").forward(req, resp);
        } else {
            req.getRequestDispatcher("/WEB-INF/views/dashboard.jsp").forward(req, resp);
        }
    }
}
